#
puts "ClassTest.rb running from " + Dir.getwd
require "EventRelay.dll"
#class EventRelay
## ----------------------------------------------------------------------------
class MyEventRelay < EventRelay
## ----------------------------------------------------------------------------
	# Virtual key codes
	VK_F1     = 112 #F1 key
	VK_LCTRL  = 308 #LCtrl key
	VK_LSHIFT = 306 #LCtrl key
	VK_CONTROL = 17 #Ctrl key

	VK_I = ?I #a key
	VK_A = 65 #a key
	VK_Y = 89 #y key
	VK_M = 77 #m key
	VK_S = ?S #s key
	VK_T = ?T #z key
	VK_Z = 90 #z key
	SUPPRESS = true
	ALLOW = false

    # ----------------------------------
    #   initialize
    # ----------------------------------
    def initialize
        @handDolly = false
        @copyModeOn = false
        #-ObjectSpace.define_finalizer(self, self.class.method(:finalize).to_proc)
        super
    end
    #-def MyEventRelay.finalize(id)
    #-    puts "Object #{id} self #{self} dying at #{Time.new}"
    #-    Logit "Object #{id} self #{self} dying at #{Time.new}"
    #-end

    # ----------------------------------
	#	OnKeyDown
    # ----------------------------------
    def OnKeyDown(key,repeat,flags)
        # puts "rb file overrode OnKeyDown"
        Logit "FROM RUBY OnKeyDown key:#{key} repeat:#{repeat} flags:#{flags}"
        vk_key, keyMacro = GetKeyInfo(key, flags)
        Logit("FROM RUBY: vk_key:#{vk_key} keyMacro:{#{keyMacro}}")

		# Kill F1 key
		if (key == VK_F1) #Kill Help key
			return SUPPRESS
		end

		# Reverse Move/Copy logic.
		if (key == VK_M) #'m'
            SendKeyMacro("{CONTROL}")
			@copyModeOn = true
			Logit("RUBY Copy on")
		end
		# Turn off Move/Copy on an alpha key
		if (@copyModeOn)
			if  ( ((key < VK_A) or (key > VK_Z)) &&
				(key != VK_LCTRL ) && (key != VK_LSHIFT) &&
				(key != VK_CONTROL )  )
				Logit("RUBY Copy off")
				@copyModeOn = false
			end
		end

		# Test timer Start
		if (key == VK_T) #'t'
			Logit("Starting Timer")
            ERTimerStart(0.5);
		end
		# Test timer Stop.
		if (key == VK_S) #'t'
			ERTimerStart(4)
			sleep(0.5)
			Logit("Stopping Timer")
            ERTimerStop();
		end
		if (key == VK_I) #'i'
            menuId = ERGetMenuID("File/Quit")
            Logit("MenuID is #{menuId}")
		end

        return ALLOW
    end
    # ----------------------------------
	#	OnKeyUp
    # ----------------------------------
    def OnKeyUp(key,repeat,flag)
        #- Logit "FROM RUBY OnKeyUp key:#{key} repeat:#{repeat} flags:#{flag}"
        return ALLOW
    end
    # ----------------------------------
	#	Mouse OnLButtonDown
    # ----------------------------------
    def OnLButtonDown(flags, x, y)
		Logit("FROM RUBY OnLButtonDown")
		if (@copyModeOn)
			Logit("OnLButtonDown Sending {LCTRL}")
			SendKeyMacro("{LCTRL}")
		end
        return ALLOW
    end
    # ----------------------------------
	#	Mouse OnLButtonUp
    # ----------------------------------
    def OnLButtonUp(flags, x, y)
        return ALLOW
    end
    # ----------------------------------
	#	Mouse OnRButtonUp
    # ----------------------------------
    def OnRButtonUp(flags, x, y)
        Logit "FROMRUBY: OnRButtonUp flags:#{flags} x:#{x} y:#{y}"
        if ( @handDolly )
            @handDolly = false
            SendKeyMacro("{MBUTTON UP}{LSHIFT UP}")
            return SUPPRESS
        end
        return ALLOW
    end #OnRButtonUp
    # ----------------------------------
	#	Mouse OnRButtonDown
    # ----------------------------------
    def OnRButtonDown(flags, x, y)
        Logit "FROMRUBY: OnRButtonDown flags:#{flags} x:#{x} y:#{y}"

		# Right Mouse dragging == Pan (Hand tool)
        x1, y1 = GetMousePosition()
        sleep(0.200)
        x2, y2 = GetMousePosition()
        if ( ((x2-x1).abs > 2) or ((y2-y1).abs > 2) )
            Logit "FROMRUBY Mouse DRAGGING"
            SendKeyMacro( "{LSHIFT DOWN}{MBUTTON DOWN}" )
            @handDolly = true
            return SUPPRESS
        end
        return ALLOW
    end #OnRButtonDown
    # ----------------------------------
	#	Menus
    # ----------------------------------
    def OnMenuSelected(menuId, menuLabel)
        Logit "FROMRUBY: OnMenuSelected MenuId:#{menuId} label:#{menuLabel}"
		#if (menuId == 57670) #Kill Help key
		#	return SUPPRESS
		#end
        return ALLOW
    end #OnMenuSelected
    # ----------------------------------
	#	OnERTimerEvent
    # ----------------------------------
    def OnERTimerEvent()
        Logit "RBUY OnERTimerEvent popped"
        return ALLOW
    end #OnERTimerEvent

end # class MyEventRelay
# ----------------------------------------------------------------------------

anEventRelay = MyEventRelay.new


